/*********************************************************************************
 *      Copyright:  (C) 2022 iot<iot@email.com>
 *                  All rights reserved.
 *
 *       Filename:  signal_PC.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(20/03/22)
 *         Author:  iot <iot@email.com>
 *      ChangeLog:  1, Release initial version on "20/03/22 14:47:38"
 *                 
 ********************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<signal.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>


int g_child_stop=0;
int g_parent_run=0;

void  sig_child(int signum)
{

	if(SIGUSR1 ==signum)
	{
	
		g_child_stop=1;
	}

}
void  sig_parent(int signum)
{

	if(SIGUSR2 ==signum)
	{
	
		g_parent_run=1;
	}
}
int main(int argc,char*argv[])
{

	int         pid;
	int         wstatus;
	signal(SIGUSR1,sig_child);
	signal(SIGUSR2,sig_parent);
	if(pid=fork()<0)
	{
	
		printf("creat child process failure%s:strerror(errno)");
		return -2;
	}
	else if(pid==0)
	{
	
		printf("child process start running and send parent a signal\n");
		kill(getppid(),SIGUSR2);
		while(!g_child_stop)
		{
		
			sleep(1);
		}
		printf("child process receive signal from parent and exit now\n");
		return 0;
	}
	printf("parent process hangs up untill receive signal from child!\n");
    while(!g_parent_run)
	{
	
		sleep(1);
	}
	printf("parnet process start running now ang send child a signal to exit\n");
	kill(pid,SIGUSR1);
	wait(&wstatus);
	printf("pareng process wait child process die and exit now\n");
	return 0;
		
}
